package com.dgshanger.sanhxiaofeisc.ui.home.activity

import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.SeekBar
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.entity.LoosePaymentEntity
import com.dgshanger.sanhxiaofeisc.entity.PaymentCodeEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.PayModelSettingContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.PayModelSettingPresenter
import com.dgshanger.sanhxiaofeisc.ui.person.setting.EditPayPasswordActivity
import com.xx.baseuilibrary.mvp.BaseMvpActivity
import com.xxl.sodmall.dialog.WalletPayDialogWrapper
import kotlinx.android.synthetic.main.activity_pay_model_setting.*
import java.math.BigDecimal
import java.text.DecimalFormat

/**
 * Created by JacobHHH on 2018/4/22.
 * 付款方式设置
 */
class PayModelSettingActivity : BaseMvpActivity<PayModelSettingContract.Model, PayModelSettingContract.View, PayModelSettingPresenter>(), PayModelSettingContract.View, View.OnClickListener {

    private lateinit var mPaymentCodeEntity: PaymentCodeEntity
    private lateinit var mPrice: String
    private var mPayType: String = "pay_wallet"
    private var node: Int = 0

    override fun createPresenter(): PayModelSettingPresenter = PayModelSettingPresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_pay_model_setting

    override fun initData() {
        mPaymentCodeEntity = intent.getSerializableExtra("PaymentCodeEntity") as PaymentCodeEntity
        mPrice = intent.getStringExtra("price")
        initView()
    }

    private var price2 = 0
    private fun initView() {
        tv_price.text = "￥" + mPrice
        btn_confirm.setOnClickListener(this)
        price2 = (mPrice.toDouble() * 100).toInt()
        Log.e("Tag", "price2=" + price2)

        node = if (price2 % 2 == 0) {
            Log.e("Tag", "整0")
            price2 / 2
        } else {
            Log.e("Tag", "非整理0")
            price2 / 2 + 1
        }
        seek_bar.max = price2

        tv_loose.setSelection(0, tv_loose.text.length)//设置选中光标

    }

    val looseTextWatcher = object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            val temp = s.toString()
            val posDot = temp.indexOf(".")
            if (posDot <= 0) return
            if (temp.length - posDot - 1 > 2) {
                s!!.delete(posDot + 3, posDot + 4)
            }
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            var loose = tv_loose.text.toString()
            //改变之后同步状态
            if (TextUtils.isEmpty(loose))
                loose = "0.00"
            val looseCount = loose.toBigDecimal()
            val coinCount = mPrice.toBigDecimal().subtract(looseCount)
            tv_coin.setText(coinCount.toPlainString())

            seek_bar.progress = looseCount.multiply(100.toBigDecimal())
                    .toInt()
        }
    }


    override fun initEvent() {

        tv_loose.addTextChangedListener(looseTextWatcher)

//        tv_coin.addTextChangedListener(coinoTextWatcher)

        seek_bar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                val scale = BigDecimal((progress * 0.01)).setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

                val scale2 = BigDecimal(price2 * 0.01).setScale(2, BigDecimal.ROUND_DOWN).toDouble()

                val scale3 = BigDecimal(scale2 - scale).setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

                /*if (progress>=node){*/
                tv_loose.setText(DecimalFormat("0.00").format(scale))
                tv_loose.setSelection(tv_loose.length())
                tv_coin.setText(DecimalFormat("0.00").format(scale3))
                /*}else if (progress<=node){
                    tv_loose.text= scale3.toString()
                    tv_coin.text= scale.toString()
                }*/
                seek_bar.progress = progress
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {

            }

            override fun onStopTrackingTouch(p0: SeekBar?) {

            }

        })

        seek_bar.progress = node
    }


    override fun onClick(view: View?) {
        when (view!!.id) {
            R.id.btn_confirm -> {
                //
                if (tv_loose.text.toString().toBigDecimal() > mPrice.toBigDecimal()) {
                    showToast("请输入正确的额度")
                    return
                }
                getPresenter().checkPwd()
            }
        }
    }

    /**
     * 弹出支付密码输入框
     */
    override fun showPwdDialog() {

        val walletPayDialogWapper = WalletPayDialogWrapper(mContext, tv_loose.text.toString().trim())
        walletPayDialogWapper.setIntegralText(tv_coin.text.toString().trim())
        walletPayDialogWapper.setOnPasswordFinishListener(object : WalletPayDialogWrapper.OnPasswordFinishListener {
            override fun onPasswordFinishListener(password: String) {

                getPresenter().goPay(mPaymentCodeEntity.show_id, mPrice, mPayType, password
                        , tv_loose.text.toString().trim(), tv_coin.text.toString().trim())
                walletPayDialogWapper.dismissDialog()
            }
        })
        walletPayDialogWapper.showDialog()
    }

    /**
     * 去设置支付密码
     */
    override fun settingPwd() {
        val intent = Intent(mContext, EditPayPasswordActivity::class.java)
        startActivity(intent)
    }

    /**
     * 支付成功
     */
    override fun onPaySuccess(entity: LoosePaymentEntity) {
        val intent = Intent(mContext, PayResultActivity::class.java)
        intent.putExtra("orderId", entity.order_sn)
        startActivity(intent)
    }

}